<?php

declare(strict_types=1);

namespace app\admin\model;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * @mixin \think\Model
 */
class Role extends Model
{


    //软删除
    use SoftDelete;
    // 数据转换为驼峰命名
    // protected $convertNameToCamel = true;
    // 设置字段信息
    // protected $schema = [
    //     'id' => 'bigint',
    //     'role_name' => 'varchar',
    //     'role_key' => 'varchar',
    //     'role_sort' => 'int',
    //     'status' => 'int',
    //     'create_time' => 'datetime',
    //     'update_time' => 'datetime',
    //     'create_by' => 'bigint',
    //     'update_by' => 'bigint',
    //     'delete_time' => 'datetime'
    // ];

    //字段映射
    // protected $mapping = [
    //     'roleKey' => 'role_key',
    //     'roleName' => 'role_name',
    //     'roleSort' => 'role_sort',
    //     'status' => 'status',
    //     'createTime' => 'create_time',
    //     'updateTime' => 'update_time',
    //     'createBy' => 'create_by',
    //     'updateBy' => 'update_by',
    //     'deleteTime' => 'delete_time'
    // ];
    protected $mapping = [
        'role_key' => 'roleKey',
        'role_name' => 'roleName',
        'role_sort' => 'roleSort',
        'create_time' => 'createTime',
        'update_time' => 'updateTime',
        'create_by' => 'createBy',
        'update_by' => 'updateBy',
        'delete_time' => 'deleteTime'
    ];
    protected $deleteTime = 'delete_time';

    //关联用户
    public function user()
    {
        return $this->belongsToMany(User::class, 'user_role');
    }

    //菜单关联
    public function menu()
    {
        return $this->belongsToMany(Menu::class, 'role_menu', 'menu_id', 'role_id');
    }

    // public function getMappedField($field)
    // {
    //     return isset($this->mapping) ? $this->mapping[$field] : $field;
    // }

    public function getMappedField($field)
    {
        $mapping =  array_search($field, $this->mapping);
        return $mapping ?? $field;
    }
}
